package utils

import (
	"image/color"

	"github.com/mojocn/base64Captcha"
)

/*
 +----------------------------------------------------------------------
 + Title        : 验证码
 + Author       : huwl
 + Version      : 1.0.0
 + Initial-Time : 2021-09-22
 + Last-time    : 这个文件最后修改的时间 + 修改人的名称
 + Desc         : 生成验证码
 +----------------------------------------------------------------------
*/

var store = base64Captcha.DefaultMemStore

// var store base64Captcha.Store = RedisStore{}

/**
 * base64版生成验证码
 *
 * @return id string, b64s string, err error
 * @author huwl
 */
func MakeCaptcha() (id string, b64s string, err error) {
	var driver base64Captcha.Driver
	//中文驱动
	// driverString := base64Captcha.DriverChinese{
	//数字英文驱动
	driverString := base64Captcha.DriverString{
		Height:          40,
		Width:           100,
		NoiseCount:      0,
		ShowLineOptions: 2 | 4,
		Length:          4,
		Source:          "123456789abcdefghijklmnopqrstuvwxyz",
		// Source: "士大夫而儿童我二哥儿童趣味发给他而合伙人特意如何无任何他让你和他",
		BgColor: &color.RGBA{
			R: 3,
			G: 102,
			B: 214,
			A: 125,
		},
		Fonts: []string{"wqy-microhei.ttc"},
	}

	driver = driverString.ConvertFonts()
	c := base64Captcha.NewCaptcha(driver, store)
	return c.Generate()
}

/**
 * 验证
 *
 * @param string id ---------------------------------------------------必传
 * @param string verifyValue ------------------------------------------必传
 * @return bool
 * @author huwl
 */
func ValidateCaptcha(id, verifyValue string) bool {
	return store.Verify(id, verifyValue, true)
}
